package com.isunland.managesystem.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.MultiLinesView;
import org.json.JSONException;

/**
 * 对象功能: 农资核查.
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/7 14:12
 */
public class OperationSubLogUpdateFragment extends BaseFragment {

  @BindView(R.id.sl_cropCheck) MultiLinesView mSlCropCheck;

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View mView = inflater.inflate(R.layout.fragment_operation_sub_log_update, container, false);
    ButterKnife.bind(this, mView);
    return mView;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(MyStringUtil.connect(R.string.add, R.string.cropCheck));
    mSlCropCheck.setTextTitle(MyStringUtil.connect(R.string.cropCheck, R.string.content));
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_confirm:
        volleyPostSave();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private void volleyPostSave() {
    if (mSlCropCheck.isEmptyContent()) {
      ToastUtil.show(R.string.completeWrite);
      return;
    }
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_UPDATE_OPERATIONSUBLOG);
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("id", mBaseParams.getId());
    params.put("checkText", mSlCropCheck.getTextContent());
    MyUtils.showDialogLoading(mActivity);
    mActivity.volleyPost(url, params.getParams(), new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {
        ToastUtil.show(R.string.failure_operation);
      }

      @Override public void onVolleyResponse(String json) throws JSONException {
        Base original = new Gson().fromJson(json, Base.class);
        ToastUtil.show(original.getMessage());
        if (original.getResult() == Base.SUCCESS) {
          mActivity.setResult(Activity.RESULT_OK);
          mActivity.finish();
        }
      }
    });
  }
}
